<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\user;

use App\Request\User\Cart\AddRequest;
use App\Service\User\CartService;
use App\Middleware\User\UserMiddleware;

use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 购物车服务
 *
 * @AutoController()
 * @Middleware(UserMiddleware::class)
 * @author 谢声强(861167322@qq.com)
 * @package App\Controller\user
 */
class CartController extends Controller
{
    /**
     * 购物车服务
     *
     * @Inject
     * @var CartService
     */
    private $service;

    /**
     * 购物车
     */
    public function get()
    {
        $result = $this->service->dao->get();
        $this->success($result);
    }

    /**
     * 添加商品 SKU 到购物车
     *
     * @param AddRequest $request
     */
    public function add(AddRequest $request)
    {
        $sku_id  = (int)$request->input('sku_id');
        $num = (int)$request->input('num');

        // 添加到购物车
        $this->service->add($sku_id, $num);

        $this->success();
    }

    /**
     * 将商品 SKU 移出购物车
     */
    public function del()
    {
        $cart_id = (int)$this->request->input('cart_id', 0);
        if (0 === $cart_id) $this->error();

        $this->service->dao->delete($cart_id);

        $this->success();
    }

    /**
     * 清空购物车
     */
    public function clear()
    {
        $this->service->dao->clear();

        $this->success();
    }
}